package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.guagifts.UserGuaData;
import com.yanqu.road.entity.qixi.UserQixiData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class UserGuaDaoImpl extends TempDataObjectDao<UserGuaData> {
    public static String getUserGuaDataSql = "select * from `t_u_gua` where `activityId`=? and `userId`=?";
    public UserGuaData getUserGuaData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserGuaDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserGuaDataSql = "update `t_u_gua` set nobleValue=?,guaValue=?,sendNobleCount=?,dayCountMap=?,lastGetTime=?,getBroadcastList=?,watchBroadcastList=?,lastWatchTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserGuaData userGuaData) {
        userGuaData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getNobleValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getGuaValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getSendNobleCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGuaData.getDayCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaData.getLastGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGuaData.getGetBroadcastList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGuaData.getWatchBroadcastList())));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaData.getLastWatchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaData.getUserId()));
        if (getDb().executeNoneQuery(updateUserGuaDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserGuaDataSql = "insert into `t_u_gua`(activityId,userId,nobleValue,guaValue,sendNobleCount,dayCountMap,lastGetTime,getBroadcastList,watchBroadcastList,lastWatchTime) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserGuaData userGuaData) {
        userGuaData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getNobleValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getGuaValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaData.getSendNobleCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGuaData.getDayCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaData.getLastGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGuaData.getGetBroadcastList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGuaData.getWatchBroadcastList())));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaData.getLastWatchTime()));
        if (getDb().executeNoneQuery(insertUserGuaDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserGuaData parseData(ResultSet rs) throws SQLException {
        UserGuaData userGuaData = new UserGuaData();
        userGuaData.setActivityId(rs.getInt("activityId"));
        userGuaData.setUserId(rs.getLong("userId"));
        userGuaData.setNobleValue(rs.getInt("nobleValue"));
        userGuaData.setGuaValue(rs.getInt("guaValue"));
        userGuaData.setSendNobleCount(rs.getInt("sendNobleCount"));
        userGuaData.setDayCountMap(JSON.parseObject(rs.getString("dayCountMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userGuaData.setLastGetTime(rs.getLong("lastGetTime"));
        userGuaData.setGetBroadcastList(JSON.parseObject(rs.getString("getBroadcastList"), new TypeReference<HashSet<Long>>(){}));
        userGuaData.setWatchBroadcastList(JSON.parseObject(rs.getString("watchBroadcastList"), new TypeReference<HashSet<Long>>(){}));
        userGuaData.setLastWatchTime(rs.getLong("lastWatchTime"));

        return userGuaData;
    }
}
